package com.keon.jianbao.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.bean.User;
import com.keon.jianbao.ui.viewpagerfragment.FriendInfoViewPagerFragment;
import com.keon.jianbao.ui.widget.CircleImageView;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONException;

import butterknife.Bind;

/**
 * Created by keon on 2015/9/6.
 */
public class FriendInfoActivity extends BaseActivity {

    FragmentManager fragmentManager;

    int user_id;

    @Bind(R.id.friends_avatar)
    CircleImageView civAvatar;
    @Bind(R.id.friends_name)
    TextView tvName;
    @Bind(R.id.friends_addr)
    TextView tvAddr;
    @Bind(R.id.friends_hobby)
    TextView tvHobby;


    SimpleHandler userInfoHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    User user = JsonUtils.fromJson(result.getContent(), User.class);

                    ImageLoader.getInstance().displayImage(user.getUser_face(), civAvatar, UILUtils.getOptions1());
                    tvName.setText(user.getUser_name());
                    if (user.getUser_gender().equals("女")) {
                        tvName.setCompoundDrawables(getResources().getDrawable(R.mipmap.icon_girl), null, null, null);
                    }
                    tvAddr.setText(user.getUser_address());
                    tvHobby.setText("爱好：" + user.getUser_hobby());


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_friend;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.friend;
    }

    @Override
    public void initView() {
        fragmentManager = getSupportFragmentManager();

    }

    @Override
    public void initData() {
        userInfoHandler.setContext(this);

        Bundle bundle = getIntent().getExtras();
        user_id = bundle.getInt("user_id");
        showWaitDialog();
        Api.user_info(AppContext.getInstance().getProperty("user.code"), user_id, userInfoHandler);

        FragmentTransaction ft = fragmentManager.beginTransaction();
        Fragment fragment = new FriendInfoViewPagerFragment();
        fragment.setArguments(bundle);
        ft.replace(R.id.friend_info_list, fragment).commit();
    }

    @Override
    public void onClick(View view) {

    }
}
